package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.*;
import com.zretc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private NationService nationService;
    @Autowired
    private PoliticsStatusService politicsStatusService;
    @Autowired
    private JobLevelService jobLevelService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/get")
    public ResponseResult getByPage(Integer page,Integer size,Employee employee, String beginDateScope1,String beginDateScope2){

        Map<String,Object> map=new HashMap<>();
        map.put("number",page);
        map.put("size",size);
        map.put("name",employee.getName());
        map.put("politicId",employee.getPoliticId());
        map.put("nationId",employee.getNationId());
        map.put("jobLevelId",employee.getJobLevelId());
        map.put("posId",employee.getPositionId());
        map.put("departmentId",employee.getDepartmentId());
        map.put("beginDateScope1",beginDateScope1);
        map.put("beginDateScope2",beginDateScope2);

        PageInfo<Employee> by = employeeService.findBy(map);

        return ResponseResult.ok("查询成功",by);
    }

    @PostMapping("/add")
    public ResponseResult add(@RequestBody Employee employee){

        int add = employeeService.add(employee);
        if (add>0){
            return ResponseResult.ok("添加成功");
        }
        return ResponseResult.ok("添加失败");
    }
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleta(@PathVariable Integer id){

        int delete = employeeService.delete(id);
        if (delete>0){
            return ResponseResult.ok("删除成功");
        }
        return ResponseResult.ok("删除失败");
    }
    @PutMapping("/update")
    public ResponseResult update(@RequestBody Employee employee){

        int update = employeeService.update(employee);
        if (update>0){
            return ResponseResult.ok("修改成功");
        }
        return ResponseResult.ok("修改失败");
    }

    @GetMapping("/getMaxWorkId")
    public ResponseResult getMaxWorkId(){

        String maxWorkId = employeeService.getMaxWorkId();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setObject(maxWorkId);
        return responseResult;
    }

    @GetMapping("/findAllNations")
    public ResponseResult findAllNations(){

        List<Nation> all = nationService.findAll();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setObject(all);
        return responseResult;
    }

    @GetMapping("/findAllPoliticsStatus")
    public ResponseResult findAllPoliticsStatus(){

        List<PoliticsStatus> all = politicsStatusService.findAll();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setObject(all);
        return responseResult;
    }
    @GetMapping("/findAllJobLevel")
    public ResponseResult findAllJobLevel(){
        List<JobLevel> all = jobLevelService.getAllJobLevels();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setObject(all);
        return responseResult;
    }

    @GetMapping("/findAllPosition")
    public ResponseResult findAllPosition(){
        List<Position> all = positionService.getAllPositions();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setObject(all);
        return responseResult;
    }

    @GetMapping("/findAllDepartment")
    public ResponseResult findAllDepartment(){
        List<Department> all = departmentService.getAllDepartmentsWithOutChildren();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setObject(all);
        return responseResult;
    }


}
